package com.sander.guardmanagement.ui.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.sander.guardmanagement.R;
import com.sander.guardmanagement.ui.model.CommuityDetailGroupModel;
import com.sander.guardmanagement.ui.model.DetailUnitModel;
import com.sander.guardmanagement.ui.view.AndroidActionSheetFragment;
import com.sander.guardmanagement.ui.view.DistrictExpandAdapter;
import com.sander.guardmanagement.widgets.AnimatedExpandableListView;

import java.util.ArrayList;
import java.util.List;

public class CommunityDetailsActivity extends AppCompatActivity {
    private AnimatedExpandableListView listView;
    private DistrictExpandAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_community_details);

        initCallBack();
        initData();
        initView();

    }

    private void initCallBack(){
        FrameLayout frameLayout = (FrameLayout) findViewById(R.id.callbackLayout);
        frameLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    private void initData(){
        List<CommuityDetailGroupModel> items = new ArrayList<CommuityDetailGroupModel>();

        // Populate our list with groups and it's children
        for(int i = 0; i < 5; i++) {

           String groupTitle = "朝阳" + i + "小区";
            List<DetailUnitModel> list = new ArrayList<>();
            for(int j = 0; j < 4; j++) {
                DetailUnitModel child = new DetailUnitModel(j + "单元");
                list.add(child);
            }
            CommuityDetailGroupModel item = new CommuityDetailGroupModel(groupTitle,list);
            items.add(item);
        }
        adapter = new DistrictExpandAdapter(this, items);

        listView = (AnimatedExpandableListView) findViewById(R.id.expand_list_view);
        listView.setAdapter(adapter);

        // In order to show animations, we need to use a custom click handler
        // for our ExpandableListView.
        listView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                // We call collapseGroupWithAnimation(int) and
                // expandGroupWithAnimation(int) to animate group
                // expansion/collapse.
                Log.d("sander","group expanded");
                if (listView.isGroupExpanded(groupPosition)) {
                    listView.collapseGroupWithAnimation(groupPosition);
                } else {
                    listView.expandGroupWithAnimation(groupPosition);
                }
                return true;
            }

        });

        listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {


            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                onClickAction(groupPosition,childPosition);

                return true;
            }
        });
    }

    private void initView(){

        Intent intent = this.getIntent();
        String district_string = intent.getStringExtra("district");
        TextView textView = (TextView) findViewById(R.id.navigation_title_view);
        textView.setText(district_string);
    }

    public void onClickAction(int groupPosition, int childPosition){

        AndroidActionSheetFragment.build(getSupportFragmentManager()).setChoice(AndroidActionSheetFragment.Builder.CHOICE.ITEM).setTitle("标题").setTag("MainActivity")
                .setItems(new String[]{"1", "2", "3", "4", "5", "6"}).setOnItemClickListener(new AndroidActionSheetFragment.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                onClickItemAction();
            }
        }).show();
    }

    public void onClickItemAction()
    {
        Intent intent = new Intent(CommunityDetailsActivity.this,SupplementaryInformationActivity.class);
        startActivity(intent);
    }

}
